package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dto.ComplaintDetailDto;
import com.example.dto.ComplaintDto;
import com.example.dto.OrderSearchDto;
import com.example.entity.Order;
import com.example.entity.OrderComplaint;
import com.example.entity.OrderItem;
import com.example.mapper.OrderItemMapper;
import com.example.mapper.OrderMapper;
import com.example.mapper.UserMapper;
import com.example.service.OrderComplaintService;
import com.example.mapper.OrderComplaintMapper;
import com.example.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 朽
* @description 针对表【order_complaint(订单投诉)】的数据库操作Service实现
* @createDate 2024-06-08 21:25:59
*/
@Service
@Slf4j
public class OrderComplaintServiceImpl extends ServiceImpl<OrderComplaintMapper, OrderComplaint>
    implements OrderComplaintService{
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Override
    public Result<List<ComplaintDto>> getOrderList() {
        ModelMapper modelMapper = new ModelMapper();
        List<OrderComplaint> orderComplaints = baseMapper.getAll();
        List<ComplaintDto> complaintDtos = new ArrayList<>();

        for (OrderComplaint orderComplaint : orderComplaints) {
            ComplaintDto complaintDto = modelMapper.map(orderComplaint, ComplaintDto.class);

            String orderSn = orderMapper.getOrderSnById(orderComplaint.getOrderId());
            complaintDto.setOrderSn(orderSn);

            String userName = userMapper.findUserNameByUserId(orderComplaint.getUserId());
            complaintDto.setUserName(userName);

            if (orderComplaint.getHandleUser() != null) {
                String handleuserName = userMapper.findUserNameByUserId(orderComplaint.getHandleUser());
                complaintDto.setHandleuserName(handleuserName);
            }

            // 状态转换
            complaintDto.setStatus(orderComplaint.getStatus());
            complaintDto.convertStatusDesc();

            complaintDtos.add(complaintDto);
        }

        return Result.ok(complaintDtos);
    }

    @Override
    public Result<List<ComplaintDto>> searchComplaint(OrderSearchDto orderSearchDto){
        List<ComplaintDto> complaintDtos = baseMapper.getsearchComplaint(orderSearchDto);
        for (ComplaintDto complaintDto: complaintDtos){
            if (complaintDto.getHandleUser() != null) {
                String handleuserName = userMapper.findUserNameByUserId(complaintDto.getHandleUser());
                complaintDto.setHandleuserName(handleuserName);
            }
            complaintDto.convertStatusDesc();
        }
        return Result.ok(complaintDtos);
    }

    @Override
    public Result<ComplaintDetailDto> getComplaintDetail(Long id){
        ModelMapper modelMapper = new ModelMapper();
        OrderComplaint orderComplaint = baseMapper.selectComplaintDetailById(id);
        if (orderComplaint == null) {
            return Result.error("投诉详情不存在");
        }
        ComplaintDetailDto complaintDetailDto = modelMapper.map(orderComplaint, ComplaintDetailDto.class);
        OrderItem orderItem = orderItemMapper.selectByOrderIdAndSkuId(orderComplaint.getOrderId(), orderComplaint.getSpuId());
        if (orderItem != null) {
            modelMapper.map(orderItem, complaintDetailDto);
        }

        Order receiver = orderMapper.selectByOrderId(orderComplaint.getOrderId());
        if (receiver != null) {
            modelMapper.map(receiver, complaintDetailDto);
        }
        complaintDetailDto.convertStatusDesc();

        return Result.ok(complaintDetailDto);
    }
}




